import smtplib
# from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr


# test_host = input("HOST: ")
# test_port = input("PORT: ")
# test_user = input("USER: ")
# test_pwd = input("PASSWORD: ")
# test_sender = 'ICES514@163.com'
# test_list = ['bjfcl20120601@163.com', '1842448061@qq.com']
# test_subject = 'Python SMTP 邮件测试'  # 邮件主题
# test_msg = MIMEText('Python 邮件发送测试', 'plain', 'utf-8')
# 邮件服务器配置
# mail_host = "smtp.163.com"  # 服务器地址
# mail_port = 994  # 服务器端口
# mail_user = "ices514@163.com"  # 发件人
# mail_pass = "NBRKGOVFTVBSQHQW"  # 发件邮箱密码

def sendmail(mail):
    mail_host = mail['host']
    mail_port = mail['port']
    mail_user = mail['user']
    mail_pass = mail['pwd']
    # 邮件配置
    mail_receiver = mail['receiver']
    mail_sender = mail['sender']
    message = mail['msg']
    message['From'] = formataddr(("EatFunFun", mail_sender))
    # message['From'] = mail_sender
    message['To'] = mail_receiver
    mail_subject = mail['subject']
    message['Subject'] = Header(mail_subject, "utf-8")
    try:
        smtpobj = smtplib.SMTP_SSL(mail_host, mail_port)
        print("111")
        smtpobj.login(mail_user, mail_pass)
        print("222")
        smtpobj.sendmail(mail_sender, mail_receiver, message.as_string())
        print("333")
        print(mail_receiver + ' send succeed!')
        return True
    except Exception as e:
        # print("jjjj")
        print(e)
        return False
